package com.nc.delivery_project.server.dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.List;

import com.nc.delivery_project.server.domain.Country;



public class CountryDao extends AbstractDao<Country>{
	
	protected final static String ENTITY_NAME = Country.class.getSimpleName();

	protected final static String COUNTRY_ID = "COUNTRY_ID";
	protected final static String COUNTRY_TITLE = "COUNTRY_TITLE";


	public CountryDao(Connection connection) throws SQLException {
		super(connection, ENTITY_NAME);
	}
	public CountryDao(){
		super(ENTITY_NAME);
	}
	
			

	@Override
	protected void prepareInsertStatement(CallableStatement statement,
			Country object) throws SQLException {
		statement.setString(1, object.getCountryTitle());
	    statement.registerOutParameter(2, Types.NUMERIC);
	    statement.setString(3, object.getCountryTitle());
		
	}


	@Override
	protected void prepareUpdateStatement(CallableStatement statement,
			Country object) throws SQLException {
		statement.setInt(1, object.getCountryId());
        statement.setString(2, object.getCountryTitle());
        statement.setString(3, object.getCountryTitle());
		
	}


	@Override
	protected List<Country> parseResultSet(ResultSet rs) throws SQLException {
		// парсин стран
		List<Country> countries = new ArrayList<>();
		
		while (rs.next()) {
	            int countryId = rs.getInt(COUNTRY_ID);
	            String countryTitle = rs.getString(COUNTRY_TITLE);
	            countries.add(new Country(countryId, countryTitle));
	        }

		return countries;
	}

	

}
